package com.ceadeal.auth.config.custom;

import com.ceadeal.auth.entity.User;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * 功能描述: 封装用户信息
 *
 * 作者: Yzw
 * 日期: 2017-03-02 11:04:14
 */
public class CustomUserDetails implements UserDetails, CredentialsContainer {

    private String username;
    private String password;
    private boolean isAccountNonExpired;
    private boolean isCredentialsNonExpired;
    private boolean isAccountNonLocked;
    private boolean isEnabled;
    private Collection<? extends GrantedAuthority> authorities;

    // ext
    private User extUserInfo;

    public CustomUserDetails(User extUserInfo, boolean isAccountNonExpired, boolean isCredentialsNonExpired, boolean isAccountNonLocked, boolean isEnabled, Collection<? extends GrantedAuthority> authorities) {
        this.extUserInfo = extUserInfo;
        this.username = extUserInfo.getUserName();
        this.password = extUserInfo.getPassword();
        this.isAccountNonExpired = isAccountNonExpired;
        this.isCredentialsNonExpired = isCredentialsNonExpired;
        this.isAccountNonLocked = isAccountNonLocked;
        this.isEnabled = isEnabled;
        this.authorities = authorities;
        this.extUserInfo.setPassword("");
    }

    @Override
    public void eraseCredentials() {

    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.isAccountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.isAccountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.isCredentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.isEnabled;
    }

    public User getExtUserInfo() {
        return extUserInfo;
    }
}